package Model;

import java.awt.*;
import java.util.Vector;

public class Equalizer extends EqualizerAnimations {

	private static final long serialVersionUID = -6412322126900940743L;
	int symmetryStyle;
	Vector<Figure> figureList = new Vector<Figure>(); 

   public void init() {
      setFrameCount(110);
   }
   
   public void drawFrame(Graphics g) {
      int frameNumber = getFrameNumber();                   
      if (frameNumber == 0)
         makeFigures();
         
      if (frameNumber >= 100) {
         g.setColor(Color.black);
         g.fillRect(0, 0, getWidth(), getHeight());
      }
      
      else {  
              
         g.setColor(getBackground());
         g.fillRect(0,0,getWidth(),getHeight());
         
         for (int i = 0; i < figureList.size(); i++) {
            Figure fig = (Figure)figureList.elementAt(i);  
            fig.drift();   
            putMultiFigure(g, fig); 
         }
         
         g.setColor(Color.black);
         g.drawRect(0,0,getWidth()-1,getHeight()-1);
      }
      
      
   }
   
   
   private void makeFigures() {
      figureList.removeAllElements();
      
      int figureCt = 1 + (int)(Math.random()*40);
      
      int figStyle = (int)(Math.random()*5);  
      
      boolean monochrome = Math.random() < 0.1; 
      
      boolean useOneColor = Math.random() < 0.15;  
      
      Color useColor = null;  
      if (useOneColor) {
         if (monochrome)
            useColor = Color.getHSBColor(0.0F, 0.0F, (float)Math.random());
         else 
            useColor = Color.getHSBColor((float)Math.random(), 1.0F, 1.0F); 
      }

      symmetryStyle = (int)(Math.random()*20);  

      if (Math.random() < 0.1)       
         setBackground(Color.white);
      else if (Math.random() < 0.11)  
         setBackground(Color.black);
      else if (monochrome)
         setBackground(Color.gray);  
      else
         setBackground(Color.getHSBColor((float)Math.random(), 0.5F, 0.5F)); 
         
      for (int i = 0; i < figureCt; i++) {
         Figure fig = new Figure();
         fig.x1 = (int)(Math.random()*getWidth());
         fig.x2 = fig.x1 + (int)(Math.random()*20) + 10; 
         fig.y1 = (int)(Math.random()*getHeight()); 
         fig.y2 = fig.y1 + (int)(Math.random()*20) + 10;  
         fig.driftx = (int)(Math.random()*13) - 6; 
         fig.drifty = (int)(Math.random()*13) - 6; 
         if (figStyle > 2)
            fig.style = (int)(Math.random()*3); 
         else
            fig.style = figStyle;
         if (useOneColor)
            fig.color = useColor;
         else if (monochrome)
            fig.color = Color.getHSBColor(0.0F, 0.0F, (float)Math.random()); 
         else
            fig.color = Color.getHSBColor((float)Math.random(), 1.0F, 1.0F);
         figureList.addElement(fig);
      }
      
   } 
   

   private void put(Graphics g, int figureType, int x1, int y1, int x2, int y2) {
      if (figureType == 0) {
         g.drawLine(x1,y1,x2,y2);
      }
      else {
         int x, y, w, h;
         if (x2 > x1) {
            x = x1;
            w = x2 - x1;
         }
         else {
            x = x2;
            w = x1 - x2;
         }
         if (y2 > y1) {
            y = y1;
            h = y2 - y1;
         }
         else {
            y = y2;
            h = y1 - y2;
         }
         if (figureType == 1)
            g.fillRect(x,y,w,h);
         else
            g.fillOval(x,y,w,h);
      }
   } 
   
   
   private void putMultiFigure(Graphics g, Figure figure) {
      g.setColor(figure.color);  
      
      put(g, figure.style, figure.x1, figure.y1,  
                           figure.x2, figure.y2);
                           
      if (symmetryStyle > 0) {  
         put(g, figure.style, getWidth() - figure.x1, figure.y1,
                              getWidth() - figure.x2, figure.y2);
      }
      
      if (symmetryStyle > 2) {
         put(g, figure.style, figure.x1, getHeight() - figure.y1,
                              figure.x2, getHeight() - figure.y2);
         put(g, figure.style, getWidth() - figure.x1, getHeight() - figure.y1,
                              getWidth() - figure.x2, getHeight() - figure.y2);
      }
      
      if (symmetryStyle > 8) {
         int a1 = (int)(((double)figure.y1 / getHeight()) * getWidth());
         int b1 = (int)(((double)figure.x1 / getWidth()) * getHeight());
         int a2 = (int)(((double)figure.y2 / getHeight()) * getWidth());
         int b2 = (int)(((double)figure.x2 / getWidth()) * getHeight());
         put(g, figure.style, a1, b1, a2, b2);
         put(g, figure.style, getWidth() - a1, b1, getWidth() - a2, b2);
         put(g, figure.style, a1, getHeight() - b1, a2, getHeight() - b2);
         put(g, figure.style, getWidth() - a1, getHeight() - b1, 
                                getWidth() - a2, getHeight() - b2);
      }
      
   } 
   

   public class Figure {
           
      int x1, x2, y1, y2; 
      
      int driftx, drifty; 
      
      int style;  

      Color color; 
      void drift() {
         x1 += driftx;
         x2 += driftx;
         y1 += drifty;
         y2 += drifty;
         if (x2 < 0) {
            x2 = getWidth() + (x2 - x1);
            x1 = getWidth();
         }
         else if (x1 > getWidth()) {
            x1 = -(x2 - x1);
            x2 = 0;
         }
         if (y2 < 0) {
            y2 = getHeight() + (y2 - y1);
            y1 = getHeight();
         }
         else if (y1 > getHeight()) {
            y1 = -(y2 - y1);
            y2 = 0;
         }
      }
      
   } 


}  

